package yja.gzw.app.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType


abstract class BaseActivity <VM:ViewModel,D: ViewDataBinding>(private val layoutid:Int):AppCompatActivity(){

    lateinit var viewmodel:VM
    lateinit var binding: D

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding= DataBindingUtil.setContentView(this,layoutid)
        val type=javaClass.genericSuperclass as ParameterizedType
        val clazz=type.actualTypeArguments[0] as Class<VM>
        viewmodel= ViewModelProvider(this)[clazz]

        initView()
        dataObservese()

    }

    abstract fun initView()

    abstract fun dataObservese()
    override fun onDestroy() {
        super.onDestroy()
        if(binding!=null)
        {
            binding.unbind()

        }

    }

}